package com.hspedu.newData.dmsxl.单调栈;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC503下一个更大元素Ⅱ {

    public int[] nextGreaterElements(int[] nums) {
        int[] res = new int[nums.length];
        for (int i = 0; i < res.length; i++) {
            res[i] = -1;
        }
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length*2 - 1; i++) {
            int j = i % nums.length;
            while(!stack.isEmpty() && nums[j] > nums[stack.peek()]){
                res[stack.pop()] = nums[j];
            }
            stack.push(j);
        }
        return res;
    }

    @Test
    public void test() {
        // 测试代码
        nextGreaterElements(new int[]{1,2,1});
        nextGreaterElements(new int[]{1,2,3,4,3});
//        nextGreaterElements(new int[]{});
    }
}
